package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //登录
    @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee){
        log.info("-------------------------");
        log.info(employee.toString());
        //生成条件选择器
        LambdaQueryWrapper<Employee> lqw =new LambdaQueryWrapper<>();
        //添加条件
        lqw.eq(Employee::getUsername,employee.getUsername());
        //查询满足条件的员工
        Employee one = employeeService.getOne(lqw);
        //判断员工是否存在
        //不存在
        if(one==null){
            return R.error("登录失败");
        }
        log.info("账户存在");
        //存在,对密码进行判断
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //密码错误
        if(!password.equals(one.getPassword())){
            return R.error("登录失败");
        }
        log.info("密码正确");
        //密码正确,判断账号是否禁用
        if(one.getStatus()==0){
            return R.error("账号已禁用");
        }
        log.info("账号未禁用");
        //账号未禁用,将员工id存入session
        session.setAttribute("employee",one.getId());
        log.info("存入session={}",session.getAttribute("employee"));
        return R.success(one);
    }


    //退出
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        //删除登录成功时保存的session
        session.removeAttribute("employeeId");
        return R.success("退出成功");
    }
    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        log.info("新增员工，员工信息：{}",employee.toString());
        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //获得当前登录用户的id
        employeeService.save(employee);
        return R.success("新增成功");
    }
    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page>page(int page, int pageSize, String name) {
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee>lqw=new LambdaQueryWrapper<>();
        //添加过滤条件
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        lqw.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }
    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        Long empId = (Long) request.getSession().getAttribute("employee");
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee>getById(@PathVariable Long id) {
        log.info("根据id查员工");
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有该员工");
    }


}
